<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item v-if="selectIndex == 0" label="选择数据项" prop="dataItemList">
                <el-select class="w100p" v-model="ruleForm.dataItemList" multiple clearable filterable placeholder="请选择数据项">
                    <el-option v-for="rtu, index in dataItemList" :label="`名称：${rtu.dataItemName}；数据项名称：${rtu.originalName}`" :value="rtu.dataItemId"
                        :key="index"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item v-if="selectIndex == 1" label="摄像头" prop="cameraNoList">
                <el-select class="w100p" v-model="ruleForm.cameraNoList" placeholder="选择摄像头" value-key="cameraNo" clearable
                    multiple filterable>
                    <el-option v-for="(item, index) in cameraList" :key="index"
                        :label="`${item.cameraId} ${item.cameraName}`" :value="item.cameraNo" />
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import deviceAllocationData from '@/api/basicInformation/deviceAllocationData.js'
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        selectIndex: {
            type: Number,
            default: () => {
                return 0
            }
        },
        deviceNo: {
            type: String,
            default: () => {
                return ''
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        cameraList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        return {
            dataItemList: [],
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {
                item1: []
            },
            rules: {
                cameraNoList: [
                    { required: true, message: '请选择摄像头', trigger: 'blur' }
                ],
                dataItemList: [
                    { required: true, message: '请选择数据项', trigger: 'blur' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    
    mounted() {
        this.getalldataitemselect();
    },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.ruleForm = { ...this.data };
                this.addEditApi = this.selectIndex == 0 ? deviceAllocationData.addlist : deviceAllocationData.addCameraList;
                this.addEditName = '添加';
            }
        },
        async getalldataitemselect() {
            let { data } = await deviceAllocationData.getalldataitemselect();
            this.dataItemList = data || [];
        },
        addMaterial() {
            let length = this.ruleForm.item4?.length || 0
            if (length) {
                this.$set(this.ruleForm.item4, length, {
                    materialName: null, num: null
                })
            }
        },
        delMaterial(index) {
            let list = this.ruleForm.item4;
            list.splice(index, 1);
            this.$set(this.ruleForm, 'item4', list)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm, deviceNo: this.deviceNo, organizationNo: sessionStorage.organizationNo }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        this.$message.warning('添加异常/重复添加')
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            setTimeout(() => {
                this.ruleForm = {};
                this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
            }, 100)
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  